package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.BiaoJuUserEscortInfo;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUnionData;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuUnionImpl extends TempDataObjectDao<CrossBiaoJuUnionData> {
    public static String getCrossBiaoJuUnionDataSql = "select * from `t_c_biaoju_union` where `activityId`=? and unionGroup=?";
    public Map<String, CrossBiaoJuUnionData> getCrossBiaoJuUnionData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossBiaoJuUnionData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossBiaoJuUnionData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossBiaoJuUnionDataSql = "update `t_c_biaoju_union` set level=?,exp=?,expChangeTime=?,rank=? where activityId=? and unionId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossBiaoJuUnionData crossBiaoJuUnionData) {
        crossBiaoJuUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUnionData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUnionData.getExpChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuUnionDataSql = "insert into `t_c_biaoju_union`(activityId,unionId,unionGroup,level,exp,expChangeTime,rank) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuUnionData crossBiaoJuUnionData) {
        crossBiaoJuUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUnionData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUnionData.getExpChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUnionData.getRank()));
        if (getDb().executeNoneQuery(insertCrossBiaoJuUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuUnionData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuUnionData crossBiaoJuUnionData = new CrossBiaoJuUnionData();
        crossBiaoJuUnionData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuUnionData.setUnionId(rs.getString("unionId"));
        crossBiaoJuUnionData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuUnionData.setLevel(rs.getInt("level"));
        crossBiaoJuUnionData.setExp(rs.getLong("exp"));
        crossBiaoJuUnionData.setExpChangeTime(rs.getLong("expChangeTime"));
        crossBiaoJuUnionData.setRank(rs.getInt("rank"));

        return crossBiaoJuUnionData;
    }
}
